package com.qxzn.app.medicalhealth.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.qxzn.app.bean.RepeatDateBean;
import com.qxzn.app.medicalhealth.R;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.MathUtil;

import java.util.List;

public class RepeatDateAdapter extends RecyclerView.Adapter<RepeatDateAdapter.ViewHolder> {

    private Context activity;
    private List<RepeatDateBean.DataBean> dataList;
    private RepeatDateBean.DataBean item;
    private int viewType;

    private View.OnClickListener checkClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (viewType == 1) {
                int position = (int) v.getTag();
                RepeatDateBean.DataBean item = dataList.get(position);
                item.setCheck(!item.isCheck());
                notifyDataSetChanged();
            }
        }
    };

    public RepeatDateAdapter(Context activity, List dataList, int viewType) {
        this.activity = activity;
        this.dataList = dataList;
        this.viewType = viewType;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (this.viewType == 2) {
            view = LayoutInflater.from(activity).inflate(R.layout.item_repeat2, parent, false);
        } else {
            view = LayoutInflater.from(activity).inflate(R.layout.item_repeat1, parent, false);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        item = dataList.get(position);
        holder.view.setTag(item);
        if (this.viewType == 2) {
            holder.et_name.setText(item.getName());
            holder.et_name.setSelection(item.getName().length());
            holder.et_name.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    AppUtil.showLog(s);
                    if (TextUtils.isEmpty(s) || (s + "").startsWith("0")) {
                        holder.et_name.setText("1");
                        holder.et_name.setSelection(holder.et_name.getText().length());
                    }
                    item.setId(MathUtil.parseInt(holder.et_name.getText().toString()));
                    item.setName(holder.et_name.getText().toString());
                }
            });
        } else {
            holder.tv_name.setText(item.getName());
            if (item.isCheck()) {
                holder.tv_name.setBackground(activity.getResources().getDrawable(R.drawable.shape_round3_blue));
                holder.tv_name.setTextColor(activity.getResources().getColor(R.color.white));
            } else {
                holder.tv_name.setBackground(activity.getResources().getDrawable(R.drawable.shape_round3_cyan));
                holder.tv_name.setTextColor(activity.getResources().getColor(R.color.black_2A2A2A));
            }
            holder.tv_name.setTag(position);
            holder.tv_name.setOnClickListener(checkClick);
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private View view;
        private TextView tv_name;
        private EditText et_name;

        ViewHolder(View view) {
            super(view);
            this.view = view;
            tv_name = view.findViewById(R.id.tv_name);
            et_name = view.findViewById(R.id.et_name);
        }
    }
}
